package com.qf.service.impl;

import com.qf.dto.OrdersDto;
import com.qf.entity.Address;
import com.qf.entity.Goods;
import com.qf.entity.Orders;
import com.qf.mapper.OrdersDao;
import com.qf.service.OrdersService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * (Orders)表服务实现类
 *
 * @author makejava
 * @since 2020-07-02 19:56:55
 */
@Service("ordersService")
public class OrdersServiceImpl implements OrdersService {
    @Resource
    private OrdersDao ordersDao;

    @Override
    public List<OrdersDto> getAllOrders() {
        return ordersDao.getAllOrders();
    }

    @Override
    public List<OrdersDto> getAllOno() {
        return ordersDao.getAllOno();
    }

    @Override
    public List<Goods> getOrderGoodsByono(String ono1) {
        return ordersDao.getOrderGoodsByono(ono1);
    }

    @Override
    public List<OrdersDto> getOrdersById(String ono) {
        return ordersDao.getOrdersById(ono);
    }

    @Override
    public String getOrdersUserById(int userId) {
        return ordersDao.getOrdersUserById(userId);
    }

    @Override
    public List<Address> getAddressByUserId(int userId) {
        return ordersDao.getAddressByUserId(userId);
    }

    @Override
    public Integer updOrderAdress(String ono, int aid) {
        return ordersDao.updOrderAdress(ono,aid);
    }

    @Override
    public List<OrdersDto> getOrders() {
        List<OrdersDto> allOno = ordersDao.getAllOno();
        Double orderscountPrice;
        List<OrdersDto> ordersDtos =new ArrayList<>();
        for (int i = 0; i < allOno.size(); i++) {
            OrdersDto ordersDto = new OrdersDto();
            List<Goods> orderGoods = null;
            orderscountPrice =0.00;
            String no1 = allOno.get(i).getNo();
            List<OrdersDto> ordersById = ordersDao.getOrdersById(no1);
            orderGoods = ordersDao.getOrderGoodsByono(no1);
            String userName = ordersDao.getOrdersUserById(ordersById.get(0).getUserId());

            for (int k = 0; k < ordersById.size(); k++) {
                Integer num = ordersById.get(k).getNum();
                String goodId1 = ordersById.get(k).getGoodId();
                for (int l = 0; l < orderGoods.size(); l++) {
                    Integer goodId = orderGoods.get(l).getGoodsId();
                    Double ordersGoodsPrice = orderGoods.get(l).getPrice();
                    String goodId2 = String.valueOf(goodId);
                    if (goodId2.equals(goodId1)) {
                        orderscountPrice = orderscountPrice+(num*ordersGoodsPrice);
                        orderGoods.get(l).setStockNum(num);//后台订单不用库存属性，所以用来存放够买数量
                    }

                }
            }


            ordersDto.setId(ordersById.get(0).getId());
            ordersDto.setNo(ordersById.get(0).getNo());
            ordersDto.setGoodPrice(orderscountPrice);
            ordersDto.setGoods(orderGoods);
            ordersDto.setNum(0);//没用
            ordersDto.setStatus(ordersById.get(0).getStatus());
            ordersDto.setAppraiseStatus(ordersById.get(0).getAppraiseStatus());
            ordersDto.setUserName(userName);
            ordersDto.setCreateTime(ordersById.get(0).getCreateTime());
            ordersDto.setPayTime(ordersById.get(0).getPayTime());
            ordersDto.setUserId(ordersById.get(0).getUserId());
            ordersDto.setGoodId("0");//没用
            ordersDto.setGoodImg("0");//没用
            ordersDto.setGoodTitle("0");//没用
            ordersDto.setOaddress(ordersById.get(0).getOaddress());
            ordersDtos.add(ordersDto);



        }


        return ordersDtos;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param oId 主键
     * @return 实例对象
     */
    @Override
    public Orders queryById(Integer oId) {
        return this.ordersDao.queryById(oId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Orders> queryAllByLimit(int offset, int limit) {
        return this.ordersDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param orders 实例对象
     * @return 实例对象
     */
    @Override
    public Orders insert(Orders orders) {
        this.ordersDao.insert(orders);
        return orders;
    }

    /**
     * 修改数据
     *
     * @param orders 实例对象
     * @return 实例对象
     */
    @Override
    public Orders update(Orders orders) {
        this.ordersDao.update(orders);
        return this.queryById(orders.getOId());
    }

    /**
     * 通过主键删除数据
     *
     * @param oId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer oId) {
        return this.ordersDao.deleteById(oId) > 0;
    }
}